using System;
using Tool.PEMath;

namespace Tool.PETimer
{
    public class LogicTimer 
    {
        public bool IsActive { get; private set; }
        Action cb;
        PEInt intervalTime;
        PEInt loopTime;//无限循环时，循环的时间间隔
        PEInt delta;

        public LogicTimer(Action cb, PEInt intervalTime, int loopTime = 0,int delta = 66) 
        {
            this.cb = cb;
            this.intervalTime = intervalTime;
            this.loopTime = loopTime;
            this.delta = delta;
            IsActive = true;
        }
    
        //创建的第二逻辑帧才会Tick 因为设置了一个bufferTimerList
        PEInt callbackCount = PEInt.zero;
        public void TickTimer() 
        {
            callbackCount += delta;
            if(callbackCount >= intervalTime && cb != null) 
            {
                cb();
                if(loopTime == 0) 
                {
                    IsActive = false;
                    cb = null;
                }
                else 
                {
                    callbackCount -= intervalTime;
                    intervalTime = loopTime;
                }
            }
        }
    }


}
